package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.adapter.TSAdapter;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.bean.TsAdapterBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ExchangeActivity extends AppCompatActivity {

    @Bind(R.id.mRedaihuitui)
    AutoLinearLayout mRedaihuitui;
    @Bind(R.id.mExchangeActivity)
    ListView mExchangeActivity;
    private int loginId1;
    private LoginHuancunBean loginHuancunBean;
    private int id;
    private TsAdapterBean tuiHuoShouhouBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_exchange);
        ButterKnife.bind(this);
        Gson gson = new Gson();
        String loginId = ACache.get(ExchangeActivity.this).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            id = loginHuancunBean.userInfo.id;
            loginId1 = loginHuancunBean.userInfo.loginId;
            setAllList(id);
        }
        setOnClick();
    }
    private void setOnClick() {
        mRedaihuitui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExchangeActivity.this.finish();
            }
        });
        mExchangeActivity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String order = String.valueOf(tuiHuoShouhouBean.refundList.get(position).orderCode);
                Intent intent=new Intent(ExchangeActivity.this,RefundDetailsActivity.class);
                intent.putExtra("order",order);
                startActivity(intent);
            }
        });
    }
    private void setAllList(int userId) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("userId", String.valueOf(userId));
        params.put("nonce", nonce);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.exchangeList, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                Log.e("退款",result.toString());
                if (result!=null){
                    String string = (String) result;
                    Gson gson = new Gson();
                    tuiHuoShouhouBean = gson.fromJson(string, TsAdapterBean.class);
                    if (tuiHuoShouhouBean.result.message.equals("未找到订单")){
                    }else {
                        if (tuiHuoShouhouBean.result.state.equals("1")){
                            mExchangeActivity.setAdapter(new TSAdapter(ExchangeActivity.this, tuiHuoShouhouBean.refundList));
                        }
                    }
                }
                ToastUtils.showShort(ExchangeActivity.this,""+tuiHuoShouhouBean.result.message);
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
                ToastUtils.showShort(ExchangeActivity.this, "失败！");
            }
        });
    }
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
}
